﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls.Maps;

namespace StylisedMap
{
    public class CloudMadeTileSource : TileSource
    {
        private const string _tilePath = "http://{S}.tile.cloudmade.com/{creds}/{style}/256/{Z}/{X}/{Y}.png";
        private readonly Random _rand = new Random();
        private readonly string[] TilePathPrefixes = new[] { "a", "b", "c" };
        
        public string CloudMadeCredentialsProvider { get; set; }
        public string CloudMadeMapStyleId { get; set; }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            string url = string.Empty;
            string prefix = string.Empty;

            prefix = TilePathPrefixes[_rand.Next(3)];
            url = _tilePath;

            //Randomize to different OSM Servers based on URL prefix
            url = url.Replace("{creds}", CloudMadeCredentialsProvider);
            url = url.Replace("{style}", CloudMadeMapStyleId);
            url = url.Replace("{S}", prefix);
            url = url.Replace("{Z}", zoomLevel.ToString());
            url = url.Replace("{X}", x.ToString());
            url = url.Replace("{Y}", y.ToString());
            return new Uri(url);
        }
        
    }
}
